<?php

    require_once("../../../includes.php");
    require_once("../model/page.php");
    require_once("../model/tags.php");
    require_once("../model/SimpleSuggest.php");
    require_once("../view/PageArchive.php");

    global $FOLIO_CFG;

    $page_ident = intval( optional_param('page', -1));
    $tag = folio_clean( optional_param('tag',''));

    if( !isloggedin() )
        trigger_error('You must be logged in to download a page', E_USER_ERROR);

    if($page_ident <> -1
       AND !in_array($_SESSION['userid'], $FOLIO_CFG->page_globaldownload) )
            trigger_error('You must be an administrator to download multiple pages',
                      E_USER_ERROR);

    //$page = folio_page::SelectWherePageIdent( $page_ident ) ;
    //if( $page == false )
        //trigger_error('Invalid page ident', E_USER_ERROR);

    // Get pages
    if( $page_ident == -1 )
        $pages = folio_SimpleSuggest::FromTag($tag);
    else
        trigger_error('Not coded', E_USER_ERROR);

    $zip = new folio_PageArchiveZip();

    if( $pages ) {
        foreach( $pages as $page ) {
            // Add to zip.
            $zip->AddPage( $page );
        }
    }

    echo $zip->toString();
?>
